
package cn.org.seventh.web.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
import org.springframework.context.support.ResourceBundleMessageSource;

/**
 * @jsp.tag name="bool"
 * @author <a href="mailto:ji.li@symbio-group.com">Andy</a>
 * @since Dec 3, 2007 10:54:17 AM
 */
public class BooleanTag extends BaseTag {

    private static final long serialVersionUID = -5739546736012138321L;

    private final transient Log log = LogFactory.getLog(BooleanTag.class);

    private static final String DEFAULT_YES = "YES";

    private static final String DEFAULT_NO = "NO";

    private Boolean value;

    private String trueValue;

    private String trueValueStyle;

    private String trueValueKey;

    private String falseValue;

    private String falseValueKey;

    private String falseValueStyle;

    /**
     * @jsp.attribute required = "false" rtexprvalue="true"
     */
    public void setFalseValue(String falseValue) {
        try {
            this.falseValue =
                (String) ExpressionEvaluatorManager.evaluate("falseValue",
                    falseValue, String.class, this, pageContext);
        } catch (JspException e) {
            log.debug(e);
        }
    }

    /**
     * @jsp.attribute required = "false"
     */
    public void setFalseValueKey(String falseValueKey) {
        this.falseValueKey = falseValueKey;
    }

    /**
     * @jsp.attribute required = "false"
     */
    public void setFalseValueStyle(String falseValueStyle) {
        this.falseValueStyle = falseValueStyle;
    }

    /**
     * @jsp.attribute required = "false" rtexprvalue="true"
     */
    public void setTrueValue(String trueValue) {
        try {
            this.trueValue =
                (String) ExpressionEvaluatorManager.evaluate("trueValue",
                    trueValue, String.class, this, pageContext);
        } catch (JspException e) {
            log.debug(e);
        }
    }

    /**
     * @jsp.attribute required = "false"
     */
    public void setTrueValueKey(String trueValueKey) {
        this.trueValueKey = trueValueKey;
    }

    /**
     * @jsp.attribute required = "false"
     */
    public void setTrueValueStyle(String trueValueStyle) {
        this.trueValueStyle = trueValueStyle;
    }

    /**
     * @jsp.attribute required = "true" rtexprvalue="true"
     */
    public void setValue(String value) {
        try {
            this.value =
                (Boolean) ExpressionEvaluatorManager.evaluate("value", value,
                    Boolean.class, this, pageContext);
        } catch (JspException e) {
            log.debug(e);
        }
    }

    public int doEndTag() throws JspException {
        JspWriter out = pageContext.getOut();
        if (value == null) {
            return super.doEndTag();
        }
        HttpServletRequest request =
            (HttpServletRequest) pageContext.getRequest();
        ResourceBundleMessageSource messageSource = getMessageSource();
        if (trueValueKey != null) {
            trueValue =
                messageSource.getMessage(trueValueKey, null, DEFAULT_YES,
                    request.getLocale());
        }
        if (falseValueKey != null) {
            falseValue =
                messageSource.getMessage(falseValueKey, null, DEFAULT_NO,
                    request.getLocale());
        }
        if (trueValue == null) {
            trueValue = DEFAULT_YES;
        }
        if (falseValue == null) {
            falseValue = DEFAULT_NO;
        }
        try {
            if (value.booleanValue()) {
                out.print("<span style=\"" + trueValueStyle + "\">");
                out.print(trueValue);
                out.print("</span>");
            } else {
                out.print("<span style=\"" + falseValueStyle + "\">");
                out.print(falseValue);
                out.print("</span>");
            }
        } catch (IOException e) {
            log.debug(e);
        }
        return super.doEndTag();
    }
}
